package com.tdogg.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tdogg.entity.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tdogg.entity.Admins;
import com.tdogg.mapper.AdminsMapper;
import com.tdogg.service.IAdminService;
import com.tdogg.util.MD5Utils;
@Service
public class AdminServiceImpl implements IAdminService {
	@Autowired
	private AdminsMapper adminsMapper;
	
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public Admins login(String name, String pass) {
		List<Admins> list = adminsMapper.findAdminByName(name);
		for (Admins a : list) {
			if(a.getAdminPass().equals(MD5Utils.passToMD5(pass+a.getAdminSalt()))){
				return a;
			}
		}
		return null;
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public Integer updateAdmin(Admins admin) {
		return adminsMapper.updateAdmin(admin);
	}
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public Admins findAdminById(Integer id) {
		return adminsMapper.findAdminById(id);
	}

	@Override
	public PageInfo<Admins> queryByPage(Integer page, Integer limit, String keywords) {
		PageHelper.startPage(page, limit);
		List<Admins> list = adminsMapper.findAllByKeywords(keywords);
		PageInfo<Admins> info=new PageInfo<Admins>(list);
		return info;
	}

	@Override
	public int updateAdminState(Integer adminId, Integer state) {
		return adminsMapper.updateAdminState(adminId,state);
	}

	@Override
	public String addAdmin(Admins admin) {
		List<Admins> list = adminsMapper.findAdminByName(admin.getAdminName());
		if(list != null && list.size() > 0){
			return "管理员名称已存在";
		}
		adminsMapper.addAdmins(admin);
		return "success";
	}

	@Override
	public int deleteAdmin(Integer adminId) {
		return adminsMapper.deleteAdmin(adminId);
	}
}
